package GUILayer;
import ModelLayer.DataHolder;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SetDate extends JFrame {

	private JPanel contentPane;
	private JTextField txtDd;
	private JLabel label;
	private JTextField txtMm;
	private JTextField txtYy;

	/**
	 * Create the frame.
	 */
	public SetDate(final String d) {
		setTitle("Set Date");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 305, 105);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblWriteDate = new JLabel("Write Date:");
		lblWriteDate.setBounds(10, 11, 279, 14);
		contentPane.add(lblWriteDate);
		
		txtDd = new JTextField();
		txtDd.setText("dd");
		txtDd.setBounds(10, 36, 40, 20);
		contentPane.add(txtDd);
		txtDd.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Date newDate = new Date();
				SimpleDateFormat formatter =  new SimpleDateFormat("ddmmyy");
    			try {
    				newDate = formatter.parse(txtDd.getText() + txtMm.getText() + txtYy.getText());
    			} catch (Exception f) {
    				JOptionPane.showMessageDialog(null,
    	                    "Date not valid!",
    	                    "Warning",
    	                    JOptionPane.WARNING_MESSAGE);
    			}
				
				if(d.equals("date"))
				{
					DataHolder.getInstance().setDateHolder(newDate);
				}
				else
				{
					DataHolder.getInstance().setDueHolder(newDate);
				}
				dispose();
			}
		});
		btnOk.setBounds(200, 35, 89, 23);
		contentPane.add(btnOk);
		
		label = new JLabel("/");
		label.setBounds(60, 39, 18, 14);
		contentPane.add(label);
		
		txtMm = new JTextField();
		txtMm.setText("mm");
		txtMm.setBounds(70, 36, 40, 20);
		contentPane.add(txtMm);
		txtMm.setColumns(10);
		
		JLabel label_1 = new JLabel("/");
		label_1.setBounds(120, 39, 18, 14);
		contentPane.add(label_1);
		
		txtYy = new JTextField();
		txtYy.setText("yy");
		txtYy.setBounds(148, 36, 40, 20);
		contentPane.add(txtYy);
		txtYy.setColumns(10);
		
	}
}
